package buaa.paas.controller;

import buaa.paas.kubectl.ApiUtil;
import buaa.paas.kubectl.DeploymentUtil;
import buaa.paas.model.Response;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.models.V1Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@CrossOrigin(origins = {"http://localhost:8080","null"})
@RestController
@RequestMapping("/deployment")
public class DeploymentController {
    @Autowired
    DeploymentUtil util;
    @Autowired
    ApiUtil apiUtil;

    /**
     * 将一个app下面的全部deployment列出
     * */
    @PostMapping("/ls")
    public Response getDeployments(@RequestParam("uid")String uid, @RequestParam("aid") String aid){
        try{
            List<TreeMap<String, Object>> result = util.listDeployment(uid+"-"+aid);
            return new Response(1001, result);
        }catch(ApiException e){
            return new Response(500, e.getMessage(), null);
        }
    }

    /**
     * 描述某个具体的deployment
     * */
    @PostMapping("/describe")
    public Response describeDeployment(@RequestParam("uid")String uid, @RequestParam("aid") String aid, @RequestParam("name")String name){
        try{
            V1Deployment d = util.findDeployment(uid+"-"+aid, name);
            return new Response(200, DeploymentUtil.describeDeployment(d));
        }catch(ApiException e){
            return new Response(500, e.getMessage(), null);
        }
    }

    /**
     * 删除一个deployment
     * */
    @PostMapping("/delete")
    public Response deleteDeployment(@RequestParam("uid")String uid, @RequestParam("aid")String aid, @RequestParam("name")String name){
        try{
            apiUtil.getApi().deleteNamespacedDeployment(name, uid+"-"+aid, null, null, null, null, null,null);
            return new Response(200, null);
        }catch(ApiException e){
            return new Response(500, e.getMessage(), null);
        }
    }

    /**
     * 创建一个deployment
     * */
    @PostMapping("/create")
    public Response createDeployment(@RequestBody Map<String, Object> args){
        try{
            int uid = (int) args.get("uid");
            int aid = (int) args.get("aid");
            int countReplicas = (int) args.get("countReplicas");
            String imageName = (String) args.get("imageName");
            String deploymentName = (String) args.get("deploymentName");
            Map<String,String> ports = (Map<String, String>) args.get("ports");
            System.out.println(ports);
            Map<String, String> labels = (Map<String, String>) args.get("labels");
            System.out.println(labels);
            Map<Integer, String>portsRemade = new TreeMap<>();
            for(String p: ports.keySet()){
                portsRemade.put(Integer.parseInt(p), ports.get(p));
            }
            System.out.println(portsRemade);
            util.createDeployment(countReplicas, imageName, deploymentName,uid+"-"+aid, portsRemade, labels);
            return new Response(200, null);
        }catch(ApiException e){
            return new Response(500, e.getMessage(), null);
        }
    }
}
